package com.lenovo.newsapp.util;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;

import com.lenovo.newsapp.R;
import com.lenovo.newsapp.activity.MainActivity;

public class Tools {
	private static final String TAG = "Tools";
	
	private static Map<String, SoftReference<Bitmap>> imageCaches = null;
	private static FileUtil fileUtil;
	private static String shortUrl = null;
//	private static Handler handler = new Handler();

	public static void setDBImg(final String imageUrl, ImageView iv) {
		final String filename = imageUrl
				.substring(imageUrl.lastIndexOf("/") + 1);
		final String filepath = fileUtil.getAbsolutePath() + "/" + filename;
		imageCaches = new HashMap<String, SoftReference<Bitmap>>();
		// 先从软引用中找
		if (imageCaches.containsKey(imageUrl)) {
			SoftReference<Bitmap> reference = imageCaches.get(imageUrl);
			Bitmap bitmap = reference.get();

			// 软引用中的 Bitmap 对象可能随时被回收
			// 如果软引用中的 Bitmap 已被回收，则从文件中找
			if (bitmap != null) {
				Log.i("aaaa", "cache exists " + filename);
				iv.setImageBitmap(bitmap);

			}
		}

		// 从文件中找
		if (fileUtil.isBitmapExists(filename)) {
			Log.i("aaaa", "file exists " + filename);
			Bitmap bitmap = BitmapFactory.decodeFile(filepath);

			// 重新加入到内存软引用中
			imageCaches.put(imageUrl, new SoftReference<Bitmap>(bitmap));

			iv.setImageBitmap(bitmap);
		}

	}

	public static void setImg(final String url, ImageView iv, Context context) {
		
		iv.setTag(url);
		iv.setImageResource(R.drawable.empty);
		AsyncImageLoader imgLoader = new AsyncImageLoader(context);
		Bitmap bitmap = imgLoader.loadImage(iv, url,
				new AsyncImageLoader.ImageDownloadCallBack() {

					@Override
					public void onImageDownloaded(ImageView imageView,
							Bitmap bitmap) {
						if (imageView != null && imageView.getTag() != null
								&& imageView.getTag().equals(url)) {
							imageView.setImageBitmap(bitmap);
						}
					}
				});

		if (bitmap != null) {
			iv.setImageBitmap(bitmap);
		}
	}
	
	public static Bitmap getBitMap(String url) {
		URL myFileUrl = null;
		Bitmap bitmap = null;
		try {
			myFileUrl = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			HttpURLConnection conn = (HttpURLConnection) myFileUrl
					.openConnection();
			conn.setDoInput(true);
			conn.connect();
			InputStream is = conn.getInputStream();
			bitmap = BitmapFactory.decodeStream(is);
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bitmap;
	}

	public static String formatDateToTime(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(date);
	}
	
}
